package com.zt.weibo.comment;

import android.content.Context;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.zt.weibo.R;
import com.zt.weibo.interfaces.IWeiboPopListener;
import com.zt.weibo.main.WeiboSendPopwin;
import com.zt.weibo.vo.CommentInfo;
import com.zt.weibo.vo.EnumSendType;

public class WeiboCommentMenuPopwin
{
	private WeiboSendPopwin mSendPopwin = null;
	private PopupWindow mPopWin = null;
	private Context mContext = null;
	private View mContentView = null;
	private View mParentView = null;
	private TextView mTitle = null;
	private TextView mReplyComment = null;
	private TextView mShowInfo = null;
	private CommentInfo mCommentInfo = null;
	
	/**
	 * 评论条目的弹出菜单
	 * @param con Context
	 * @param parentView 父View
	 * @param listener 回调接口，设置给发布弹窗。
	 */
	public WeiboCommentMenuPopwin(Context con, View parentView, IWeiboPopListener listener)
	{
		mContext = con;
		mParentView = parentView;
		mContentView = LayoutInflater.from(mContext).inflate(R.layout.weibo_comment_press_menu, null);
		mPopWin = new PopupWindow(mContentView, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		mPopWin.setFocusable(true);
		mPopWin.setOutsideTouchable(false);
		mPopWin.setAnimationStyle(R.style.popwin_animation);
		mTitle = (TextView) mContentView.findViewById(R.id.comment_menu_title);
		mReplyComment = (TextView) mContentView.findViewById(R.id.comment_menu_comment);
		mShowInfo = (TextView) mContentView.findViewById(R.id.comment_menu_userinfo);
		setListeners();
		mSendPopwin = new WeiboSendPopwin(mContext, mParentView, listener);
	}
	
	private void setListeners()
	{
		ClickListener clickListener = new ClickListener();
		TouchListener touchListener = new TouchListener();
		KeyListener keyListener = new KeyListener();
		mReplyComment.setOnClickListener(clickListener);
		mReplyComment.setOnTouchListener(touchListener);
		mShowInfo.setOnClickListener(clickListener);
		mShowInfo.setOnTouchListener(touchListener);
		mTitle.setOnKeyListener(keyListener);
	}
	
	/**
	 * 显示菜单，顺便传入评论数据类。
	 */
	public void show(CommentInfo commentInfo)
	{
		mTitle.requestFocus();
		mCommentInfo = commentInfo;
		try
		{
			mPopWin.showAtLocation(mParentView, Gravity.CENTER, 0, 0);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	/**
	 * 消隐菜单
	 */
	public void dismiss()
	{
		try
		{
			if (null != mPopWin && mPopWin.isShowing())
			{
				mPopWin.dismiss();
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	private class ClickListener implements OnClickListener
	{
		@Override
		public void onClick(View v)
		{
			switch (v.getId())
			{
			case R.id.comment_menu_comment:
				mSendPopwin.setSendType(EnumSendType.E_SEND_REPLAY, mCommentInfo);
				mSendPopwin.show();
				dismiss();
				break;
			case R.id.comment_menu_userinfo:
				Toast.makeText(mContext, mContext.getString(R.string.toast_not_finish), 1000).show();
				dismiss();
				break;
			}
		}
	}
	
	private class TouchListener implements OnTouchListener
	{
		@Override
		public boolean onTouch(View v, MotionEvent event)
		{
			v.requestFocus();
			return false;
		}
	}

	private class KeyListener implements OnKeyListener
	{
		@Override
		public boolean onKey(View v, int keyCode, KeyEvent event)
		{
			if (keyCode == KeyEvent.KEYCODE_BACK)
			{
				dismiss();
				return true;
			}
			return false;
		}
	}
	
}
